package com.miaomiaojia.waterfallcache;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import com.miaomiaojia.android.BuildConfig;


import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

public class CacheUtils {
	public final static String TAG = "cache";
	private CacheUtils() {
	};

	public static final int HTTP_CACHE_SIZE = 10 * 1024 * 1024; // 10MB
	public static final String HTTP_CACHE_DIR = "http";
	public static final int DEFAULT_CACHE_FILE_SIZE = 10 * 512; // 512 kb
	public static final String IMAGE_CACHE_DIR = "thumbs";
	public static final int IO_BUFFER_SIZE = 8 * 1024;
	@SuppressLint("NewApi")
	public static long getUsableSpace(File path) {
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
//			Debug("2.3->"+ path.getUsableSpace());
//			return path.getUsableSpace();
//		}
//		Debug("2.1->"+ path.getUsableSpace());
		final StatFs stats = new StatFs(path.getPath());
		return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
	}


	@SuppressLint("NewApi")
	public static boolean isExternalStorageRemovable() {
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
//			return Environment.isExternalStorageRemovable();
//		}
		return false;
	}

	public static boolean hasExternalCacheDir() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
	}

	@SuppressLint("NewApi")
	public static File getExternalCacheDir(Context context) {
		if (hasExternalCacheDir()) {
			Debug("dir->" + context.getExternalCacheDir());
			return context.getExternalCacheDir();
		}

		final String cacheDir = "/Android/data/" + context.getPackageName()
				+ "/cache/";
		return new File(Environment.getExternalStorageDirectory().getPath()
				+ cacheDir);
	}
	
	/**
	 * Check if OS version has a http URLConnection bug. See here for more
	 * information:
	 * http://android-developers.blogspot.com/2011/09/androids-http-clients.html
	 * 
	 * @return
	 */
	public static boolean hasHttpConnectionBug() {
		// return Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO;
		return Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR;
	}
	
	/**
	 * Workaround for bug pre-Froyo, see here for more info:
	 * http://android-developers.blogspot.com/2011/09/androids-http-clients.html
	 */
	public static void disableConnectionReuseIfNecessary() {
		// HTTP connection reuse which was buggy pre-froyo
		if (hasHttpConnectionBug()) {
			System.setProperty("http.keepAlive", "false");
		}
	}
	
	public static void CopyStream(InputStream in, OutputStream out) {
		final int buffer_size = 1024;
		try {
			byte[] bytes = new byte[buffer_size];
			for (;;) {
				int count = in.read(bytes, 0, buffer_size);
				if (count == -1)
					break;
				out.write(bytes, 0, count);
			}
		} catch (Exception ex) {
		}

		// 鐎圭偟骞囨禍锟�		// int b;
		// while ((b = in.read()) != -1) {
		// out.write(b);
		// }

	}
	
	 /**
     * Get the memory class of this device (approx. per-app memory limit)
     *
     * @param context
     * @return
     */
    public static int getMemoryClass(Context context) {
        return ((ActivityManager) context.getSystemService(
                Context.ACTIVITY_SERVICE)).getMemoryClass();
    }
	public static void Debug(String message){
		if(BuildConfig.DEBUG){
			Log.d(TAG, message);
		}
	}

}
